var path = require('path');
var fs = require('fs');
var watch = require('watch');

var isDir = (baseName) => {
    var ext = path.extname(baseName);
    if (ext === '') {
        return true;
    } else {
        return false;
    }
}


var requireDir = (dir) => {
    var files = fs.readdirSync(dir);
    files.forEach((dirOrFile) => {
        if (path.extname(dirOrFile) === '') { // dir
            var abPath = path.format({
                dir: dir,
                base: dirOrFile
            })
            ///loadFileWithDir(abPath) 不遍历子节点了，手动，比较安全
        } else { // file
            var url = path.format({ dir: dir, base: dirOrFile })
            try {
                require(url)
                console.log("'" + dirOrFile + "'" + ' module is mounted !')
            } catch (e) {
                console.error(e);
            }
        }
    })
};

var requireDirRecursive = (dir) => {
    var files = fs.readdirSync(dir);
    files.forEach((dirOrFile) => {
        if (path.extname(dirOrFile) === '') { // dir
            var abPath = path.format({
                dir: dir,
                base: dirOrFile
            })
            requireDirRecursive(abPath)
        } else { // file
            var url = path.format({ dir: dir, base: dirOrFile })
            try {
                require(url)
                console.log("'" + dirOrFile + "'" + ' module is mounted !')
            } catch (e) {
                console.error(e);
            }
        }
    })
};


// 监听文件变化，增加路由或更新路由 (热更新只监听业务逻辑)
// watch.createMonitor(path.format({
//     dir: __dirname,
//     base: '/src/api'
// }), function (monitor) {
//     // monitor.files['./test/watch/floor/t2.js'] // Stat object for my zshrc.
//     monitor.on("created", function (f, stat) {
//         var baseName = path.basename(f);
//         if (isDir(baseName)) return;

//         // new files
//         try {
//             baseName = path.basename(f, '.js')
//             require(f);
//             console.log(dateHelper.now() + "  '" + f + "'" + ' New module !')
//         } catch (e) {
//             console.error(e);
//         }
//     });

//     monitor.on("changed", function (f, curr, prev) {
//         var baseName = path.basename(f);
//         if (isDir(baseName)) return;

//         // new files
//         baseName = path.basename(f, '.js')
//         var mounts = server.router.mounts;
//         for (let i in mounts) {
//             if (i.indexOf(baseName) > -1) {
//                 server.rm(i);
//                 console.log(dateHelper.now() + "  '" + baseName + "'" + " router removed !")
//             }
//         }
//         try {
//             delete require.cache[f];
//             require(f);
//             console.log("'" + baseName + "'" + " is mounted !")
//         } catch (e) {
//             console.error(e);
//         }
//     });

//     monitor.on("removed", function (f, stat) {
//         var baseName = path.basename(f);
//         if (isDir(baseName)) return;
//         baseName = path.basename(f, '.js')
//         server.rm(baseName.toLowerCase());
//         delete require.cache[f];
//         console.log("'" + baseName + "'" + " router removed !")
//     })
// })

module.exports = {
    requireDir: requireDir,
    requireDirRecursive: requireDirRecursive
}